/*
    Kelompok    2
    Kelas       2
    Anggota :
    1. Yudha Wastu Prawira      / 13509002
    2. Robertus Theodore        / 13509008
    3. Rendy Bambang Junior     / 13509036
    4. Hartono Sulaiman Wijaya  / 13509046
    5. Daniel Prihartoni        / 13509088
    6. Raydhitya Yosep          / 13509092

*/
#ifndef _RESOURCE_H
#define _RESOURCE_H

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include "Constant.h"

using namespace std;

typedef struct
{
	int x;
	int y;
} Point;


//==================================================
//Game
//--------------------------------------------------
//infos needed to display
typedef int StateType;

//==================================================
//Plant
//--------------------------------------------------
//plant type
typedef int HarvestType;
typedef int FruitType;
typedef int PlantType;
typedef int SeedType;

//==================================================
//Store and Items
//--------------------------------------------------
//type store
typedef int ItemType; //see Constant.h
typedef struct{
	string name;
	int price;
} StoreElem;
typedef vector<StoreElem> StoreInfo;

//==================================================
//Farmer
//--------------------------------------------------
typedef int AreaType;
typedef int Direction;


//==================================================
//Player
//--------------------------------------------------

//type inventory
typedef struct
{
	ItemType item;
	int amount;
} InventoryElem;
typedef vector< InventoryElem > Inventory;

//type time
typedef int Time;

//package buat save
typedef struct
{
	std::string name;
	AreaType curArea;
	Point curPos;
	Direction curDir; //nambahin direction
	Time timeLeft;
	int balance;
	Inventory myInventory;
	int day;
} PlayerInfo;

//==================================================
//Dwarf
//--------------------------------------------------
typedef int DwarfType;
typedef int DwarfStatus;
typedef struct
{
	DwarfType type;
	DwarfStatus status;
	Point curPos;
	Direction curDir;
} DwarfInfo;
typedef DwarfInfo AllDwarfInfo[3];
//==================================================
//Plant
//--------------------------------------------------
typedef int PlantType;
typedef int HappyMeter;
typedef int AgeLeft;
typedef struct
{
	PlantType PT;
	HappyMeter HM;
	AgeLeft AL;
	Point Position;
} PlantElem;
typedef PlantElem AllPlant[21];
//--------------------------------------------------
//Farm
//--------------------------------------------------
typedef Point Plantable[121] ;
typedef Point Plowed[121];
typedef Point Watered[121];
typedef Point Filled[121];
typedef Point NoAccess[121];
typedef struct
{
	Plantable plantable;
	Plowed plowed;
	Watered watered;
	NoAccess noacc;
} FarmPlanInfo;
//==================================================
//Save
//--------------------------------------------------
typedef struct
{
	//berisi semua yang perlu di save
	PlayerInfo PI;
	AllDwarfInfo ADI;
	AllPlant AP;
	FarmPlanInfo FP;
	int status;
} SaveInfo;

//==================================================
//Resource Class
//--------------------------------------------------
class Resource{
	public :
		//singleton ctor
		static Resource* instance();
		void initialize();

		//method
		SaveInfo loadGame(string);
		void saveGame(SaveInfo); //Save all game data to a file. if it hasn't been saved, prompt filename to save. use SAVE_FILE_EXTENSION constant

		void startNewGame(); //construct and initialize all game objects with default value
		void startLoadedGame(); //construct and initialize all game objects with loaded file
		void exitGame(); //prompt whether player want to save the game or not, destruct all

		//Character Machine
		void start ();
		/*  I.S. : Sembarang */
		/*  F.S. : CC adalah karakter pertama yang dibaca pada pita */
		/*	Jika CC = MARK, EOP akan true */
		/*	Jika CC != MARK, EOP akan false */
		void adv ();
		/*  I.S. : karakter pada jendela adalah CC, retval != EOF */
		/*  F.S. : CC merupakan karakter berikutnya dari CC yang lama */
		/*  Jika EOF, file ditutup */

		//Word Machine
		void startWord ();
		/*  I.S. : Sembarang */
		/*  F.S. : CC adalah karakter pertama yang dibaca pada pita */
		/*  Jika CC = MARK, EOP akan true */
		/*  Jika CC != MARK, EOP akan false */

		void copyWord ();
		/*  I.S. : CC merupakan karakter pertama dari Word */
		/*  F.S. : CC = BLANK atau CC = MARK */
		/*         CC adalah karakter berikutnya dari CSel yang lama */
		/*         S berisi Sel yang sudah diproses */

		void advAttrib();

		void copyDwarf();
		void copyPlant();
		void copyFarm();
		int countPlant(SaveInfo SI);
		int countPlantable(SaveInfo SI);
		int countPlowed(SaveInfo SI);
		int countWatered(SaveInfo SI);

		StoreInfo getStoreInfo();
		string getItemName(int);
		SaveInfo getSaveInfo();
		ItemType getItemCode(string);
		map<string,ItemType> itemData;

		SaveInfo SI; //atribut public untuk kemudahan pengetesan
        string externalFile;
	private :
		//hidden ctor,cctor,= & dtor
		Resource();
		~Resource();
		bool isSaved();
		//string savedFileName;
		char CC;
		bool EndWord;
		char S[100];

		std::ifstream loadHarpesMun;
		std::ofstream saveHarpesMun;
		StoreInfo storeInfo;
		vector<string> itemName;
};

#endif
